﻿using AutoMapper;
using Volo.Abp.Data;
using Xms.Abp.Data;
using Xms.Abp.MultiTenancy;

namespace TenantManagement;

public class TenantManagementDomainMappingProfile : Profile
{
    public TenantManagementDomainMappingProfile()
    {
        CreateMap<Tenant, TenantData>()
            .ForMember(ti => ti.ConnectionStrings, opts =>
            {
                opts.MapFrom((tenant, ti) =>
                {
                    var connStrings = new ConnectionStrings();

                    foreach (var connectionString in tenant.ConnectionStrings)
                    {
                        connStrings[connectionString.Name] = connectionString.Value;
                    }

                    return connStrings;
                });
            })
            .ForMember(ti => ti.Domains, opts =>
            {
                opts.MapFrom((tenant, ti) =>
                {
                    var domains = new Domains();

                    foreach (var domain in tenant.Domains)
                    {
                        domains[domain.Name] = domain.Value;
                    }

                    return domains;
                });
            })
            .ForMember(r=>r.IsActive, opts => opts.MapFrom((tenant, r) => tenant.IsEnabled));

        CreateMap<Tenant, TenantEto>();
    }
}
